package com.ibuonline.b2b.webservice.hessian;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;

import com.dimeng.commons.dao.Page;
import com.dimeng.sip.entity.Member;
import com.dimeng.sip.service.MemberService;
import com.ibuonline.b2b.common.SysCache;
import com.ibuonline.b2b.member.dao.MemberDAO;

public class MemberServiceImpl implements MemberService {

	private MemberDAO memberDAO;
	
	@Override
	public Page<Member> findMembers(Page<Member> page) {
		List<String> paramNames = new ArrayList<String>();
		List<Object> paramValues = new ArrayList<Object>();
		Map<String, Object> params =  page.getFilters();
		StringBuffer queryHql = new StringBuffer("from Member where 1=1 ");
		StringBuffer countHql = new StringBuffer("select count(*) from Member where 1=1 ");
		if(params != null) {
			Set<String> columns = params.keySet();
			for(String column:columns) {
				String[] condition = column.split(":");
				if("eq".equals(condition[0])) {
					queryHql.append(" and ")
					  .append(condition[1])
					  .append("=? ");
					
					countHql.append(" and ")
					  .append(condition[1])
					  .append("=? ");
					paramNames.add(condition[1]);
					paramValues.add(params.get(column));
				} else if("like".equals(condition[0])) {
					queryHql.append(" and ")
					  .append(condition[1])
					  .append(" like ? ");
					
					countHql.append(" and ")
					  .append(condition[1])
					  .append(" like ? ");
					paramNames.add(condition[1]);
					paramValues.add("%"+params.get(column)+"%");
				}
				
			}
		}
		
		queryHql.append(" order by createTime desc");
		if(page.isAutoCount()) {
			String[] columns = new String[paramNames.size()];
			paramNames.toArray(columns);
			page.setTotalCount(memberDAO.getRecordCount(countHql.toString(), paramValues.toArray()));
		}
		List<com.ibuonline.b2b.entity.Member> members = memberDAO.query(queryHql.toString(), paramValues.toArray(), page.getFirst(), page.getPageSize());
		List<Member> sipMembers = new ArrayList<Member>();
		for(com.ibuonline.b2b.entity.Member member:members) {
			sipMembers.add(toSipMember(member));
		}
		page.setResult(sipMembers);
		return page;
	}

	@Override
	public Member getMemberByEmail(String email) {
		List<com.ibuonline.b2b.entity.Member> members = memberDAO.getByParam("email", email);
		if(members.size() > 0) {
			return toSipMember(members.get(0));
		}
		return null;
	}

	@Override
	public Member getMemberByLoginName(String loginName) {
		List<com.ibuonline.b2b.entity.Member> members = memberDAO.getByParam("loginName", loginName);
		if(members.size() > 0) {
			com.ibuonline.b2b.entity.Member member = members.get(0);
			member.setMobile(SysCache.getShopDomainByCompanyId(member.getMemberId()));
			return toSipMember(member);
		}
		return null;
	}
	
	protected Member toSipMember(
			com.ibuonline.b2b.entity.Member entityMember) {
		if (entityMember != null) {
			Member member = new Member();
			BeanUtils.copyProperties(entityMember, member);
			return member;
		} else {
			return null;
		}
	}

	public MemberDAO getMemberDAO() {
		return memberDAO;
	}

	public void setMemberDAO(MemberDAO memberDAO) {
		this.memberDAO = memberDAO;
	}
}
